package com.tcoiss.datafactory.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcoiss.common.core.domain.R;
import com.tcoiss.common.core.utils.DateUtils;
import com.tcoiss.common.component.api.RemoteApiService;
import com.tcoiss.common.component.api.model.ApiParam;
import com.tcoiss.datafactory.domain.KTrans;
import com.tcoiss.datafactory.mapper.KTransMapper;
import com.tcoiss.datafactory.service.IKTransService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 转换Service业务层处理
 *
 * @author zw
 * @date 2021-06-01
 */
@Service
public class KTransServiceImpl extends ServiceImpl<KTransMapper, KTrans> implements IKTransService {

    @Resource
    private RemoteApiService remoteApiService;

    @Override
    public List<KTrans> queryList(KTrans kTrans) {
        List<KTrans> kTranList = new ArrayList<>();
        Map<String, Object> kTMap = new HashMap<>();
        ApiParam apiUrl = new ApiParam();
        kTMap.put("size",1000);
        apiUrl.setParam(kTMap);
        apiUrl.setApiCode("ETLTransQuery");
        R<Map<String, Object>> response = remoteApiService.executeApi(apiUrl);
        if(response.getCode() == 200) {
            Map<String, Object> mapData = response.getData();
            if ((Boolean) mapData.get("success") == true) {
                Map<String, Object> resultMap = (Map<String, Object>) mapData.get("result");
                if ((Integer) resultMap.get("size") <= 0) {
                    return kTranList;
                }
                List<Map<String,Object>> contentListMap = (List<Map<String, Object>>) resultMap.get("content");
                for (int i = 0; i < contentListMap.size(); i++){
                    Map<String, Object> ktMap = contentListMap.get(i);
                    KTrans kt = new KTrans();
                    kt.setId(Long.valueOf((Integer) ktMap.get("id")));
                    kt.setCategoryId((Integer) ktMap.get("categoryId"));
                    kt.setTransName((String) ktMap.get("transName"));
                    kt.setTransDescription((String) ktMap.get("transDescription"));
                    kt.setTransType((String) ktMap.get("transType"));
                    kt.setTransPath((String) ktMap.get("transPath"));
                    kt.setAddTime(DateUtils.strParseToDate(ktMap.get("addTime")));
                    kt.setDelFlag((Integer) ktMap.get("delFlag"));
                    if(ktMap.get("transParams") != null){
                        kt.setTransParams(ktMap.get("transParams").toString());
                    }
                    kTranList.add(kt);
                }
                return kTranList;
            }
        }
        return null;
    }

    @Override
    public  Map<String, Object> startTransById(Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        ApiParam apiUrl = new ApiParam();
        apiUrl.setParam(map);
        apiUrl.setApiCode("ETLStartTrans");
        R<Map<String, Object>> response = remoteApiService.executeApi(apiUrl);
        Map<String, Object> mapData = response.getData();
        return mapData;
    }
}
